import datetime

from src.utils.config import logger
from src.utils.db_processor import mysql_prediction_processor
from src.utils.util import get_today, check_date_str, format_date_string

ANOMALY_WINDOW_DAYS = 60


def load_anomaly_document_item(start_date=None):
    """

    :param start_date: 起始日期
    :return:
    """
    if start_date is None:
        start_date = get_today() - datetime.timedelta(days=ANOMALY_WINDOW_DAYS)
    else:
        start_date = check_date_str(start_date)

    logger.info('loading anomaly product no start_date@{}'.format(start_date))
    sql = """
    SELECT mapping.document_item_id
    FROM tablet_anomaly_one_day_detection detection 
    JOIN tablet_anomaly_one_day_mapping mapping ON detection.lookup_key =  mapping.lookup_key
    WHERE (detection.audit_status IS NULL OR detection.audit_status = 0) AND detection.create_time >= '{}'
    """.format(format_date_string(start_date))

    return mysql_prediction_processor.load_sql(sql)
